using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class BattleChatbox : MonoBehaviour
{
	public Transform TextboxObject;

	public Image CharacterPortrait;

	public TextMeshProUGUI TextUI;

	public TextMeshProUGUI TextBulletpointUI;

	public AudioSource TextVoiceEmitter;

	private string CurrentText = "";

	public int CurrentTextIndex;

	public int CurrentAdditionalTextIndex;

	private int Text_SubtractedRichText;

	private int StoredAfterIndex;

	public int PauseCounter;

	public bool FinishedShowingReactions;

	public CHATBOXTEXT storedchatboxtext;

	public CHATBOXTEXT previouschatboxtext;

	public bool ChatIsCurrentlyRunning;

	public bool AllowInput = true;

	private INT_Chat storedreciever;

	public List<string> StoredAdditiveValues = new List<string>();

	[SerializeField]
	private Transform ReactionHolder;

	public GameObject ReactionTemplate;

	private List<GameObject> StoredReactions = new List<GameObject>();

	public TMP_FontAsset DefaultFont;

	public TMP_FontAsset DyslexicFont;

	public Image[] ChatboxImages;

	public Sprite Kojima;

	public AudioClip DefaultSpeakSound;

	public Sprite DefaultIcon;

	public RectTransform[] Options;

	public RectTransform HeartCursor;

	public bool ChatboxInteractDebounce;

	private int ChoiceNumber;

	public int PreviousChosenChoiceIndex;

	private int cursorpos;

	private bool CurrentlyInChoice;

	private bool HideChoicesUntilFinish;

	private float CurrentTextSpeedMultiplier;

	public bool TextVisible = true;

	private float TalkIconTimer;

	private float TalkIconInterval = 1f / 6f;

	private static BattleChatbox instance;

	public static BattleChatbox Instance => instance;

	private void Awake()
	{
		if ((Object)(object)instance != (Object)null && (Object)(object)instance != (Object)(object)this)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
		}
		else
		{
			instance = this;
		}
	}

	private void Update()
	{
		if (!GonerMenu.Instance.gonerMenuWasOpen && !GonerMenu.Instance.GonerMenuOpen)
		{
			ProcessInput();
		}
		TalkingIconProcess();
		SetHeartCursorPOS();
		if (StoredReactions.Count > 0 && !ChatIsCurrentlyRunning)
		{
			ClearReactions();
		}
		if (TextVisible)
		{
			((Behaviour)TextUI).enabled = true;
			((Behaviour)TextBulletpointUI).enabled = true;
			((Behaviour)CharacterPortrait).enabled = true;
			SetVisibilityOfReactions(Visible: true);
		}
		else
		{
			((Behaviour)TextUI).enabled = false;
			((Behaviour)TextBulletpointUI).enabled = false;
			((Behaviour)CharacterPortrait).enabled = false;
			SetVisibilityOfReactions(Visible: false);
		}
	}

	private void ProcessInput()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0304: Unknown result type (might be due to invalid IL or missing references)
		//IL_0376: Unknown result type (might be due to invalid IL or missing references)
		//IL_0482: Unknown result type (might be due to invalid IL or missing references)
		bool key = Input.GetKey(PlayerInput.Instance.Key_Menu);
		if (CurrentText != "" || CurrentText != null)
		{
			PauseCounter = FormatCurrentText(CurrentText, IncludeBulletPoint: false, ActivateNextDialogueCharacter: false, ConvertSpecialCharacters: false).Length - Text_SubtractedRichText;
		}
		if (CurrentlyInChoice && ((TMP_Text)TextUI).maxVisibleCharacters == PauseCounter && HideChoicesUntilFinish && !((Component)HeartCursor).gameObject.activeSelf)
		{
			((Component)Options[0]).gameObject.SetActive(true);
			((Component)Options[1]).gameObject.SetActive(true);
			((Component)Options[2]).gameObject.SetActive(true);
			((Component)Options[3]).gameObject.SetActive(true);
			((Component)HeartCursor).gameObject.SetActive(true);
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Left) && CurrentlyInChoice && AllowInput && cursorpos - 1 > -1)
		{
			cursorpos--;
			SetHeartCursorPOS();
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Right) && CurrentlyInChoice && AllowInput && cursorpos + 1 <= ChoiceNumber - 1)
		{
			cursorpos++;
			SetHeartCursorPOS();
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) && FinishedShowingReactions && CurrentlyInChoice && ((TMP_Text)TextUI).maxVisibleCharacters == PauseCounter && AllowInput)
		{
			ClearReactions();
			if ((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Choice[CurrentTextIndex] != (Object)null && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Choice[CurrentTextIndex].ChoiceTextResults[cursorpos] == (Object)null)
			{
				PreviousChosenChoiceIndex = cursorpos;
				if (CurrentTextIndex + 1 != storedchatboxtext.Textboxes[StoredAfterIndex].Text.Length && !LightworldMenu.Instance.MenuOpen && !DarkworldMenu.Instance.MenuOpen && (Object)(object)storedchatboxtext != (Object)null)
				{
					CurrentlyInChoice = false;
					CurrentTextIndex++;
					RunText(storedchatboxtext, StoredAfterIndex, storedreciever, ResetCurrentTextIndex: false);
					MonoBehaviour.print((object)"test");
				}
				else
				{
					EndText();
				}
			}
			else
			{
				PreviousChosenChoiceIndex = cursorpos;
				CurrentlyInChoice = false;
				RunText(storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Choice[CurrentTextIndex].ChoiceTextResults[cursorpos], 0, storedreciever, ResetCurrentTextIndex: true);
			}
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel) && CurrentlyInChoice && AllowInput && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Choice[CurrentTextIndex] != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Choice[CurrentTextIndex].CanBackOut)
		{
			EndText();
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) && !CurrentTextHasSkip(CurrentText) && FinishedShowingReactions && ((TMP_Text)TextUI).maxVisibleCharacters == PauseCounter && !LightworldMenu.Instance.MenuOpen && !DarkworldMenu.Instance.MenuOpen && (Object)(object)storedchatboxtext != (Object)null && !CurrentlyInChoice && AllowInput)
		{
			AttemptRunActions();
			AttemptRunSubActions(IsChatEnd: true);
			if ((Object)(object)storedchatboxtext != (Object)null && storedchatboxtext.Textboxes[StoredAfterIndex] != null && CurrentTextIndex + 1 != storedchatboxtext.Textboxes[StoredAfterIndex].Text.Length)
			{
				CurrentTextIndex++;
				RunText(storedchatboxtext, StoredAfterIndex, storedreciever, ResetCurrentTextIndex: false);
			}
			else
			{
				EndText();
			}
		}
		if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel) && !CurrentTextHasSkip(CurrentText) && !DarkworldMenu.Instance.MenuOpen && !LightworldMenu.Instance.MenuOpen && ((TMP_Text)TextUI).maxVisibleCharacters != PauseCounter && (Object)(object)storedchatboxtext != (Object)null && AllowInput)
		{
			((MonoBehaviour)this).StopCoroutine("PlayText");
			FinishCurrentText();
		}
		if (!key || LightworldMenu.Instance.MenuOpen || !((Object)(object)storedchatboxtext != (Object)null) || !AllowInput)
		{
			return;
		}
		AttemptRunActions();
		AttemptRunSubActions(IsChatEnd: true);
		if (!CurrentlyInChoice)
		{
			if (storedchatboxtext.Textboxes[StoredAfterIndex] != null && CurrentTextIndex + 1 != storedchatboxtext.Textboxes[StoredAfterIndex].Text.Length)
			{
				CurrentTextIndex++;
				if ((Object)(object)storedreciever != (Object)null)
				{
					RunText(storedchatboxtext, StoredAfterIndex, storedreciever, ResetCurrentTextIndex: false);
				}
				else
				{
					RunText(storedchatboxtext, StoredAfterIndex, null, ResetCurrentTextIndex: false);
				}
			}
			else
			{
				EndText();
			}
		}
		else
		{
			((MonoBehaviour)this).StopCoroutine("PlayText");
			FinishCurrentText();
		}
	}

	private bool CurrentTextHasSkip(string Text)
	{
		if (Text == null || Text == "")
		{
			return false;
		}
		if (Text.Contains('£'))
		{
			return true;
		}
		return false;
	}

	private string FormatCurrentText(string TargetText, bool IncludeBulletPoint, bool ActivateNextDialogueCharacter, bool ConvertSpecialCharacters = true)
	{
		string text = TargetText;
		while (text.Contains('|'))
		{
			int num = text.IndexOf('|');
			int num2 = num + 1;
			int i;
			for (i = num2; i < text.Length && char.IsDigit(text[i]); i++)
			{
			}
			if (int.TryParse(text.Substring(num2, i - num2), out var result) && result >= 0 && result < StoredAdditiveValues.Count)
			{
				string value = StoredAdditiveValues[result];
				text = text.Remove(num, i - num);
				text = text.Insert(num, value);
				continue;
			}
			Debug.LogError((object)"Invalid index after '|' or index out of range.");
			break;
		}
		if (IncludeBulletPoint)
		{
			char[] array = text.ToCharArray();
			foreach (char num3 in array)
			{
				if (num3 == ';')
				{
					MonoBehaviour.print((object)"DETECTED");
					if ((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex] != (Object)null)
					{
						if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].GiveCharacterBulletpoint)
						{
							TextMeshProUGUI textBulletpointUI = TextBulletpointUI;
							((TMP_Text)textBulletpointUI).text = ((TMP_Text)textBulletpointUI).text + "\n*";
						}
						else
						{
							TextMeshProUGUI textBulletpointUI2 = TextBulletpointUI;
							((TMP_Text)textBulletpointUI2).text = ((TMP_Text)textBulletpointUI2).text + "\n";
						}
					}
					else
					{
						TextMeshProUGUI textBulletpointUI3 = TextBulletpointUI;
						((TMP_Text)textBulletpointUI3).text = ((TMP_Text)textBulletpointUI3).text + "\n*";
					}
				}
				if (num3 != '~')
				{
					continue;
				}
				MonoBehaviour.print((object)"DETECTED");
				if ((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex] != (Object)null)
				{
					if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].GiveCharacterBulletpoint)
					{
						TextMeshProUGUI textBulletpointUI4 = TextBulletpointUI;
						((TMP_Text)textBulletpointUI4).text = ((TMP_Text)textBulletpointUI4).text + "\n\n*";
					}
					else
					{
						TextMeshProUGUI textBulletpointUI5 = TextBulletpointUI;
						((TMP_Text)textBulletpointUI5).text = ((TMP_Text)textBulletpointUI5).text + "\n";
					}
				}
				else
				{
					TextMeshProUGUI textBulletpointUI6 = TextBulletpointUI;
					((TMP_Text)textBulletpointUI6).text = ((TMP_Text)textBulletpointUI6).text + "\n\n*";
				}
			}
		}
		if (ConvertSpecialCharacters)
		{
			text = Regex.Replace(text, "(?<!\\=)#", "");
			text = text.Replace("@", "");
			text = text.Replace(";", "\n");
			text = text.Replace("~", "\n");
		}
		if (text.Contains('£') && ActivateNextDialogueCharacter)
		{
			if ((Object)(object)storedchatboxtext != (Object)null && storedchatboxtext.Textboxes[StoredAfterIndex] != null && CurrentTextIndex + 1 != storedchatboxtext.Textboxes[StoredAfterIndex].Text.Length)
			{
				CurrentTextIndex++;
				RunText(storedchatboxtext, StoredAfterIndex, storedreciever, ResetCurrentTextIndex: false);
			}
			else
			{
				EndText();
			}
		}
		if (ConvertSpecialCharacters)
		{
			text = text.Replace("£", "");
		}
		return text;
	}

	private void FinishCurrentText()
	{
		if ((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex] != (Object)null)
		{
			if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].GiveCharacterBulletpoint)
			{
				((TMP_Text)TextBulletpointUI).text = "*";
			}
			else
			{
				((TMP_Text)TextBulletpointUI).text = "";
			}
		}
		else
		{
			((TMP_Text)TextBulletpointUI).text = "*";
		}
		if ((Object)(object)storedreciever != (Object)null)
		{
			storedreciever.CurrentlyBeingUsed = false;
		}
		((TMP_Text)TextUI).text = FormatCurrentText(storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text[CurrentTextIndex], IncludeBulletPoint: true, ActivateNextDialogueCharacter: true);
		StartShowingReactions();
		Text_SubtractedRichText = CountRichTextTagCharacters(((TMP_Text)TextUI).text);
		((TMP_Text)TextUI).maxVisibleCharacters = ((TMP_Text)TextUI).text.Length - Text_SubtractedRichText;
	}

	private void StartShowingReactions()
	{
		((MonoBehaviour)this).StartCoroutine(ShowCurrentTextReactions());
	}

	private IEnumerator ShowCurrentTextReactions()
	{
		FinishedShowingReactions = false;
		if ((Object)(object)storedchatboxtext == (Object)null || !ChatIsCurrentlyRunning || storedchatboxtext.Textboxes == null || storedchatboxtext.Textboxes.Length == 0)
		{
			FinishedShowingReactions = true;
			yield break;
		}
		Textbox textbox = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex];
		if (textbox == null || textbox.Reaction == null || textbox.Reaction.Length == 0)
		{
			FinishedShowingReactions = true;
			yield break;
		}
		CHATBOXREACTION cHATBOXREACTION = textbox.Reaction[CurrentTextIndex];
		if ((Object)(object)cHATBOXREACTION == (Object)null || cHATBOXREACTION.Reaction == null)
		{
			FinishedShowingReactions = true;
			yield break;
		}
		REACTIONDATA[] reaction = cHATBOXREACTION.Reaction;
		foreach (REACTIONDATA rEACTIONDATA in reaction)
		{
			if (rEACTIONDATA == null || (Object)(object)rEACTIONDATA.Character == (Object)null)
			{
				continue;
			}
			GameObject val = Object.Instantiate<GameObject>(ReactionTemplate, ReactionHolder);
			if ((Object)(object)val == (Object)null)
			{
				continue;
			}
			Image componentInChildren = val.GetComponentInChildren<Image>();
			TextMeshProUGUI componentInChildren2 = val.GetComponentInChildren<TextMeshProUGUI>();
			if ((Object)(object)componentInChildren2 == (Object)null || (Object)(object)componentInChildren == (Object)null)
			{
				Object.Destroy((Object)(object)val);
				continue;
			}
			val.SetActive(true);
			((Transform)val.GetComponent<RectTransform>()).localPosition = Vector2.op_Implicit(rEACTIONDATA.Offset);
			((TMP_Text)componentInChildren2).text = rEACTIONDATA.Text;
			((TMP_Text)componentInChildren2).fontSize = rEACTIONDATA.Character.CharacterFontSize / 2f;
			((Graphic)componentInChildren2).color = rEACTIONDATA.Character.TextColor;
			if (PlayerPrefs.GetInt("Setting_DyslexicText", 0) == 0)
			{
				((TMP_Text)componentInChildren2).font = (((Object)(object)rEACTIONDATA.Character.CharacterFont != (Object)null) ? rEACTIONDATA.Character.CharacterFont : DefaultFont);
			}
			else
			{
				((TMP_Text)componentInChildren2).font = DyslexicFont;
			}
			if ((Object)(object)rEACTIONDATA.Character.CharacterIcon != (Object)null)
			{
				componentInChildren.sprite = rEACTIONDATA.Character.CharacterIcon;
				((Graphic)componentInChildren).rectTransform.sizeDelta = new Vector2(rEACTIONDATA.Character.CharacterIconWidth / 2f, rEACTIONDATA.Character.CharacterIconHeight / 2f);
			}
			else
			{
				componentInChildren.sprite = DefaultIcon;
			}
			StoredReactions.Add(val);
			yield return (object)new WaitForSeconds(0.05f);
		}
		FinishedShowingReactions = true;
	}

	private void SetVisibilityOfReactions(bool Visible)
	{
		foreach (GameObject storedReaction in StoredReactions)
		{
			Image componentInChildren = storedReaction.GetComponentInChildren<Image>();
			TextMeshProUGUI componentInChildren2 = storedReaction.GetComponentInChildren<TextMeshProUGUI>();
			((Behaviour)componentInChildren).enabled = Visible;
			((Behaviour)componentInChildren2).enabled = Visible;
		}
	}

	private void ClearReactions()
	{
		((MonoBehaviour)this).StopCoroutine("ShowCurrentTextReactions");
		foreach (GameObject storedReaction in StoredReactions)
		{
			Object.Destroy((Object)(object)storedReaction);
		}
		StoredReactions.Clear();
	}

	private void TalkingIconProcess()
	{
		TalkIconTimer += Time.deltaTime;
		if (!ChatIsCurrentlyRunning)
		{
			return;
		}
		if (((TMP_Text)TextUI).maxVisibleCharacters < PauseCounter)
		{
			if ((Object)(object)storedchatboxtext != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] != null && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex] != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterHasTalkingAnimation && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterIcon != (Object)null && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterTalkingIcon != (Object)null && TalkIconTimer >= TalkIconInterval)
			{
				MonoBehaviour.print((object)"SUCCESS!");
				if ((Object)(object)CharacterPortrait.sprite == (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterIcon)
				{
					CharacterPortrait.sprite = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterTalkingIcon;
				}
				else
				{
					CharacterPortrait.sprite = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterIcon;
				}
				TalkIconTimer = 0f;
			}
		}
		else if ((Object)(object)storedchatboxtext != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] != null && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex] != (Object)null && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterIcon != (Object)null)
		{
			CharacterPortrait.sprite = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].CharacterIcon;
		}
	}

	private void AttemptRunActions()
	{
		Debug.Log((object)"AttemptRunActions started.");
		if (storedchatboxtext.Textboxes == null)
		{
			Debug.LogError((object)"storedchatboxtext.Textboxes is null");
			return;
		}
		if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] == null)
		{
			Debug.LogError((object)"storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] is null");
			return;
		}
		Textbox textbox = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex];
		if (textbox.Action == null)
		{
			Debug.LogError((object)"currentTextbox.Action is null");
			return;
		}
		if (textbox.Action.Length == 0)
		{
			Debug.Log((object)"currentTextbox.Action.Length is 0");
			return;
		}
		if ((Object)(object)textbox.Action[CurrentTextIndex] == (Object)null)
		{
			Debug.Log((object)"currentTextbox.Action[CurrentTextIndex] is null");
			return;
		}
		CHATBOXACTION cHATBOXACTION = textbox.Action[CurrentTextIndex];
		if (!cHATBOXACTION.RunActionOnChatEnd)
		{
			Debug.Log((object)"action.RunActionOnChatEnd is false");
			return;
		}
		Debug.Log((object)("Action will be run: " + ((object)cHATBOXACTION).ToString()));
		if (cHATBOXACTION.PlaySound)
		{
			if (cHATBOXACTION.PossibleSounds == null)
			{
				Debug.LogError((object)"action.PossibleSounds is null");
				return;
			}
			if (cHATBOXACTION.PossibleSounds.Length == 0)
			{
				Debug.LogError((object)"action.PossibleSounds.Length is 0");
				return;
			}
			AudioClip val = cHATBOXACTION.PossibleSounds[Random.Range(0, cHATBOXACTION.PossibleSounds.Length)];
			Debug.Log((object)("Playing sound: " + ((Object)val).name));
			TextVoiceEmitter.PlayOneShot(val);
		}
		if (cHATBOXACTION.RunComponentFunction)
		{
			if (cHATBOXACTION.TargetComponentGameObjectName == null)
			{
				Debug.LogError((object)"action.TargetComponentGameObjectName is null");
				return;
			}
			GameObject val2 = GameObject.Find(cHATBOXACTION.TargetComponentGameObjectName);
			if ((Object)(object)val2 == (Object)null)
			{
				Debug.LogError((object)("TargetGameObject not found: " + cHATBOXACTION.TargetComponentGameObjectName));
				return;
			}
			if (cHATBOXACTION.FunctionName == null)
			{
				Debug.LogError((object)"action.FunctionName is null");
				return;
			}
			string targetComponentClassname = cHATBOXACTION.TargetComponentClassname;
			if (targetComponentClassname == null)
			{
				Debug.LogError((object)"action.TargetComponentClassname is null");
				return;
			}
			Component component = val2.GetComponent(targetComponentClassname);
			if ((Object)(object)component == (Object)null)
			{
				Debug.LogError((object)("Couldn't find Component named: " + targetComponentClassname));
				return;
			}
			MethodInfo method = ((object)component).GetType().GetMethod(cHATBOXACTION.FunctionName);
			if (method == null)
			{
				Debug.LogError((object)("Method not found: " + cHATBOXACTION.FunctionName));
				return;
			}
			Debug.Log((object)("Invoking method: " + cHATBOXACTION.FunctionName));
			method.Invoke(component, null);
		}
		Debug.Log((object)"AttemptRunActions ended.");
	}

	private void AttemptRunSubActions(bool IsChatEnd = false)
	{
		if ((Object)(object)storedchatboxtext == (Object)null)
		{
			return;
		}
		if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] == null)
		{
			Debug.LogError((object)"storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] is null");
			return;
		}
		Textbox textbox = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex];
		if (textbox.Action == null)
		{
			Debug.LogError((object)"currentTextbox.Action is null");
			return;
		}
		if (textbox.Action.Length == 0)
		{
			Debug.Log((object)"currentTextbox.Action.Length is 0");
			return;
		}
		if ((Object)(object)textbox.Action[CurrentTextIndex] == (Object)null)
		{
			Debug.Log((object)"currentTextbox.Action[CurrentTextIndex] is null");
			return;
		}
		CHATBOXACTION obj = textbox.Action[CurrentTextIndex];
		CHATBOXACTION_SubAction[] subActions = obj.SubActions;
		if (obj.RunComponentFunction && subActions != null && subActions.Length != 0)
		{
			CHATBOXACTION_SubAction[] array = subActions;
			foreach (CHATBOXACTION_SubAction cHATBOXACTION_SubAction in array)
			{
				if (cHATBOXACTION_SubAction.RunActionOnChatEnd != IsChatEnd)
				{
					return;
				}
				if (cHATBOXACTION_SubAction.TargetComponentGameObjectName == null)
				{
					Debug.LogError((object)"action.TargetComponentGameObjectName is null");
					return;
				}
				GameObject val = GameObject.Find(cHATBOXACTION_SubAction.TargetComponentGameObjectName);
				if ((Object)(object)val == (Object)null)
				{
					Debug.LogError((object)("TargetGameObject not found: " + cHATBOXACTION_SubAction.TargetComponentGameObjectName));
					return;
				}
				if (cHATBOXACTION_SubAction.FunctionName == null)
				{
					Debug.LogError((object)"action.FunctionName is null");
					return;
				}
				string targetComponentClassname = cHATBOXACTION_SubAction.TargetComponentClassname;
				if (targetComponentClassname == null)
				{
					Debug.LogError((object)"action.TargetComponentClassname is null");
					return;
				}
				Component component = val.GetComponent(targetComponentClassname);
				if ((Object)(object)component == (Object)null)
				{
					Debug.LogError((object)("Couldn't find Component named: " + targetComponentClassname));
					return;
				}
				MethodInfo method = ((object)component).GetType().GetMethod(cHATBOXACTION_SubAction.FunctionName);
				if (method == null)
				{
					Debug.LogError((object)("Method not found: " + cHATBOXACTION_SubAction.FunctionName));
					return;
				}
				Debug.Log((object)("Invoking method: " + cHATBOXACTION_SubAction.FunctionName));
				method.Invoke(component, null);
			}
		}
		Debug.Log((object)"AttemptRunActions ended.");
	}

	private void SetupChoices(CHATBOXTEXT chatbox, int index)
	{
		//IL_0539: Unknown result type (might be due to invalid IL or missing references)
		//IL_053e: Unknown result type (might be due to invalid IL or missing references)
		//IL_055a: Unknown result type (might be due to invalid IL or missing references)
		//IL_055f: Unknown result type (might be due to invalid IL or missing references)
		//IL_057b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0580: Unknown result type (might be due to invalid IL or missing references)
		//IL_059c: Unknown result type (might be due to invalid IL or missing references)
		//IL_05a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_029b: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02db: Unknown result type (might be due to invalid IL or missing references)
		//IL_0315: Unknown result type (might be due to invalid IL or missing references)
		//IL_031a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0336: Unknown result type (might be due to invalid IL or missing references)
		//IL_033b: Unknown result type (might be due to invalid IL or missing references)
		//IL_036a: Unknown result type (might be due to invalid IL or missing references)
		//IL_036f: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_041f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0424: Unknown result type (might be due to invalid IL or missing references)
		//IL_01de: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0219: Unknown result type (might be due to invalid IL or missing references)
		//IL_021e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0258: Unknown result type (might be due to invalid IL or missing references)
		//IL_025d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0279: Unknown result type (might be due to invalid IL or missing references)
		//IL_027e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0448: Unknown result type (might be due to invalid IL or missing references)
		//IL_044d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0483: Unknown result type (might be due to invalid IL or missing references)
		//IL_0488: Unknown result type (might be due to invalid IL or missing references)
		//IL_04be: Unknown result type (might be due to invalid IL or missing references)
		//IL_04c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_04f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_04fe: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)chatbox.Textboxes[index].Choice[CurrentTextIndex] != (Object)null)
		{
			CurrentlyInChoice = true;
			CHATBOXCHOICE cHATBOXCHOICE = chatbox.Textboxes[index].Choice[CurrentTextIndex];
			ChoiceNumber = cHATBOXCHOICE.Choices.Count;
			SetHeartCursorPOS();
			float num = -82.5f;
			if (chatbox.Textboxes[index].Choice[CurrentTextIndex].ShowOnTextScrollFinish)
			{
				((Component)Options[0]).gameObject.SetActive(false);
				((Component)Options[1]).gameObject.SetActive(false);
				((Component)Options[2]).gameObject.SetActive(false);
				((Component)Options[3]).gameObject.SetActive(false);
				((Component)HeartCursor).gameObject.SetActive(false);
				HideChoicesUntilFinish = true;
			}
			else
			{
				((Component)Options[0]).gameObject.SetActive(true);
				((Component)Options[1]).gameObject.SetActive(true);
				((Component)Options[2]).gameObject.SetActive(true);
				((Component)Options[3]).gameObject.SetActive(true);
				((Component)HeartCursor).gameObject.SetActive(true);
				HideChoicesUntilFinish = false;
			}
			num = ((!(chatbox.Textboxes[index].Text[CurrentTextIndex] == "") && chatbox.Textboxes[index].Text[CurrentTextIndex] != null) ? (-82.5f) : 82.5f);
			if (ChoiceNumber == 2)
			{
				if ((Object)(object)chatbox.Textboxes[index].Character[CurrentTextIndex] != (Object)null && (Object)(object)chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterIcon != (Object)null)
				{
					((Transform)Options[0]).localPosition = Vector2.op_Implicit(new Vector2(-150f, num));
					((TMP_Text)((Component)Options[0]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[0];
					((Transform)Options[1]).localPosition = Vector2.op_Implicit(new Vector2(150f, num));
					((TMP_Text)((Component)Options[1]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[1];
					((Transform)Options[2]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
					((Transform)Options[3]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
				}
				else
				{
					((Transform)Options[0]).localPosition = Vector2.op_Implicit(new Vector2(-253.4f, num));
					((TMP_Text)((Component)Options[0]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[0];
					((Transform)Options[1]).localPosition = Vector2.op_Implicit(new Vector2(46.6f, num));
					((TMP_Text)((Component)Options[1]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[1];
					((Transform)Options[2]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
					((Transform)Options[3]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
				}
			}
			else
			{
				_ = ChoiceNumber;
				_ = 2;
			}
			if (ChoiceNumber == 3)
			{
				((Transform)Options[0]).localPosition = Vector2.op_Implicit(new Vector2(-200f, num));
				((TMP_Text)((Component)Options[0]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[0];
				((Transform)Options[1]).localPosition = Vector2.op_Implicit(new Vector2(0f, num));
				((TMP_Text)((Component)Options[1]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[1];
				((Transform)Options[2]).localPosition = Vector2.op_Implicit(new Vector2(200f, num));
				((TMP_Text)((Component)Options[2]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[2];
				((Transform)Options[3]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
			}
			if (ChoiceNumber == 4)
			{
				((Transform)Options[0]).localPosition = Vector2.op_Implicit(new Vector2(-400f, num));
				((TMP_Text)((Component)Options[0]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[0];
				((Transform)Options[1]).localPosition = Vector2.op_Implicit(new Vector2(-150f, num));
				((TMP_Text)((Component)Options[1]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[1];
				((Transform)Options[2]).localPosition = Vector2.op_Implicit(new Vector2(150f, num));
				((TMP_Text)((Component)Options[2]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[2];
				((Transform)Options[3]).localPosition = Vector2.op_Implicit(new Vector2(400f, num));
				((TMP_Text)((Component)Options[3]).GetComponent<TextMeshProUGUI>()).text = cHATBOXCHOICE.Choices[3];
			}
		}
		else
		{
			((Transform)Options[0]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
			((Transform)Options[1]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
			((Transform)Options[2]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
			((Transform)Options[3]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		}
	}

	private void CleanupOptions()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		((Transform)Options[0]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		((Transform)Options[1]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		((Transform)Options[2]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		((Transform)Options[3]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		((Component)HeartCursor).gameObject.SetActive(false);
	}

	private void SetHeartCursorPOS()
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		((Transform)HeartCursor).localPosition = Vector2.op_Implicit(new Vector2(((Transform)Options[cursorpos]).localPosition.x - 35f, ((Transform)Options[cursorpos]).localPosition.y));
		for (int i = 0; i < Options.Length; i++)
		{
			if (i != cursorpos)
			{
				((Graphic)((Component)Options[i]).GetComponent<TextMeshProUGUI>()).color = Color.white;
			}
			else
			{
				((Graphic)((Component)Options[i]).GetComponent<TextMeshProUGUI>()).color = new Color(0.996f, 1f, 0f);
			}
		}
		if (ChoiceNumber < 0)
		{
			((Transform)HeartCursor).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		}
	}

	private void SetTextboxPosY(float PosY)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		TextboxObject.localPosition = Vector2.op_Implicit(new Vector2(TextboxObject.localPosition.x, PosY));
	}

	public void RunText(CHATBOXTEXT Chatbox, int index, INT_Chat reciever, bool ResetCurrentTextIndex)
	{
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_014f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0173: Unknown result type (might be due to invalid IL or missing references)
		//IL_0197: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0262: Unknown result type (might be due to invalid IL or missing references)
		//IL_0286: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0418: Unknown result type (might be due to invalid IL or missing references)
		//IL_042c: Unknown result type (might be due to invalid IL or missing references)
		if (ResetCurrentTextIndex)
		{
			CurrentTextIndex = 0;
		}
		((MonoBehaviour)this).StopCoroutine("PlayText");
		CleanupOptions();
		ClearReactions();
		ChatIsCurrentlyRunning = true;
		CurrentAdditionalTextIndex = index;
		CurrentTextSpeedMultiplier = 1f;
		((Graphic)TextUI).color = Color.white;
		ChatboxInteractDebounce = true;
		Text_SubtractedRichText = 0;
		Image[] chatboxImages = ChatboxImages;
		for (int i = 0; i < chatboxImages.Length; i++)
		{
			((Behaviour)chatboxImages[i]).enabled = true;
		}
		if (Chatbox.Textboxes[index].Character.Length != 0 && (Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex] == (Object)null)
		{
			CharacterPortrait.sprite = DefaultIcon;
			((TMP_Text)TextUI).margin = new Vector4(120f, 95f, 120f, 10f);
			((TMP_Text)TextBulletpointUI).margin = new Vector4(60f, 95f, 1180f, 10f);
		}
		else
		{
			Transform transform = ((Component)CharacterPortrait).transform;
			((RectTransform)((transform is RectTransform) ? transform : null)).sizeDelta = new Vector2(Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterIconWidth * 0.9f, Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterIconHeight * 0.9f);
			((TMP_Text)TextUI).margin = new Vector4(290f, 95f, 120f, 10f);
			((TMP_Text)TextBulletpointUI).margin = new Vector4(240f, 95f, 980f, 10f);
			Random.Range(0, 10000);
			CurrentTextSpeedMultiplier = Chatbox.Textboxes[index].Character[CurrentTextIndex].TextSpeedMultiplier;
			((Graphic)TextUI).color = Chatbox.Textboxes[index].Character[CurrentTextIndex].TextColor;
			if ((Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterIcon != (Object)null)
			{
				CharacterPortrait.sprite = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterIcon;
			}
			else
			{
				CharacterPortrait.sprite = DefaultIcon;
				((TMP_Text)TextUI).margin = new Vector4(120f, 95f, 120f, 10f);
				((TMP_Text)TextBulletpointUI).margin = new Vector4(60f, 95f, 1180f, 10f);
			}
		}
		if ((Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex] == (Object)null || (Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterSound == (Object)null)
		{
			TextVoiceEmitter.clip = DefaultSpeakSound;
		}
		else
		{
			TextVoiceEmitter.clip = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterSound;
		}
		if (Chatbox.Textboxes[index] != null && Chatbox.Textboxes[index].Choice.Length != 0 && (Object)(object)Chatbox.Textboxes[index].Choice[CurrentTextIndex] != (Object)null)
		{
			SetupChoices(Chatbox, index);
		}
		else
		{
			ChoiceNumber = 0;
		}
		((Behaviour)TextUI).enabled = true;
		((TMP_Text)TextUI).text = "";
		StoredAfterIndex = index;
		storedchatboxtext = Chatbox;
		if ((Object)(object)reciever != (Object)null)
		{
			storedreciever = reciever;
			if ((Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex] != (Object)null && Chatbox.Textboxes[index].Character[CurrentTextIndex].TellRecieverIfChatting)
			{
				reciever.CurrentlyBeingUsed = true;
			}
			else
			{
				reciever.CurrentlyBeingUsed = false;
			}
		}
		cursorpos = 0;
		if ((Object)(object)Camera.main != (Object)null && (Object)(object)PlayerManager.Instance != (Object)null && (Object)(object)storedreciever != (Object)null)
		{
			if (!storedreciever.ManualTextboxPosition)
			{
				if (((Component)PlayerManager.Instance).transform.position.y >= ((Component)Camera.main).transform.position.y)
				{
					SetTextboxPosY(-280f);
				}
				else
				{
					SetTextboxPosY(280f);
				}
			}
			else if (storedreciever.OnBottom)
			{
				SetTextboxPosY(-280f);
			}
			else
			{
				SetTextboxPosY(280f);
			}
		}
		if (PlayerPrefs.GetInt("Setting_DyslexicText", 0) == 1)
		{
			((TMP_Text)TextUI).font = DyslexicFont;
			((TMP_Text)TextBulletpointUI).font = DefaultFont;
		}
		else if (PlayerPrefs.GetInt("Setting_NoFont", 0) == 0)
		{
			if ((Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex] != (Object)null && (Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFont != (Object)null)
			{
				((TMP_Text)TextUI).font = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFont;
				((TMP_Text)TextBulletpointUI).font = ((TMP_Text)TextUI).font;
			}
			else
			{
				((TMP_Text)TextUI).font = DefaultFont;
				((TMP_Text)TextBulletpointUI).font = DefaultFont;
			}
		}
		else
		{
			((TMP_Text)TextUI).font = DefaultFont;
			((TMP_Text)TextBulletpointUI).font = DefaultFont;
		}
		if ((Object)(object)Chatbox.Textboxes[index].Character[CurrentTextIndex] != (Object)null && Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFontSize != 0f)
		{
			((TMP_Text)TextUI).fontSize = Chatbox.Textboxes[index].Character[CurrentTextIndex].CharacterFontSize;
		}
		else
		{
			((TMP_Text)TextUI).fontSize = 64f;
		}
		if (((Component)this).gameObject.activeInHierarchy)
		{
			((MonoBehaviour)this).StartCoroutine("PlayText");
		}
	}

	public void EndText()
	{
		//IL_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Unknown result type (might be due to invalid IL or missing references)
		//IL_015a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		Image[] chatboxImages = ChatboxImages;
		for (int i = 0; i < chatboxImages.Length; i++)
		{
			((Behaviour)chatboxImages[i]).enabled = false;
		}
		previouschatboxtext = storedchatboxtext;
		if ((Object)(object)storedreciever != (Object)null)
		{
			storedreciever.FinishedText = true;
		}
		((Behaviour)TextUI).enabled = false;
		StoredAfterIndex = 0;
		storedchatboxtext = null;
		CurrentText = "";
		((TMP_Text)TextUI).text = "";
		((TMP_Text)TextBulletpointUI).text = "";
		((Behaviour)CharacterPortrait).enabled = false;
		CharacterPortrait.sprite = DefaultIcon;
		PauseCounter = 0;
		cursorpos = 0;
		ChoiceNumber = 0;
		Text_SubtractedRichText = 0;
		ChatIsCurrentlyRunning = false;
		((MonoBehaviour)this).StartCoroutine(DebounceDelay());
		ClearReactions();
		((Component)HeartCursor).gameObject.SetActive(false);
		SetHeartCursorPOS();
		CurrentTextIndex = 0;
		CurrentlyInChoice = false;
		((Transform)Options[0]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		((Transform)Options[1]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		((Transform)Options[2]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		((Transform)Options[3]).localPosition = Vector2.op_Implicit(new Vector2(0f, 1500f));
		CurrentAdditionalTextIndex = 0;
		if ((Object)(object)storedreciever != (Object)null)
		{
			((MonoBehaviour)this).StartCoroutine(storedreciever.DebounceInteract());
			storedreciever.CurrentlyBeingUsed = false;
		}
		storedreciever = null;
		((MonoBehaviour)this).StopCoroutine("PlayText");
	}

	private IEnumerator DebounceDelay()
	{
		yield return (object)new WaitForSeconds(0.01f);
		ChatboxInteractDebounce = false;
	}

	private IEnumerator PlayText()
	{
		string StoredText = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text[CurrentTextIndex];
		((TMP_Text)TextUI).text = StoredText;
		if ((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex] != (Object)null)
		{
			if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].GiveCharacterBulletpoint)
			{
				((TMP_Text)TextBulletpointUI).text = "*";
			}
			else
			{
				((TMP_Text)TextBulletpointUI).text = "";
			}
		}
		else
		{
			((TMP_Text)TextBulletpointUI).text = "*";
		}
		CurrentText = StoredText;
		((TMP_Text)TextUI).maxVisibleCharacters = 0;
		int MaxVisibleCharacters = 0;
		Text_SubtractedRichText = 0;
		bool ForcedFinishText = false;
		int messageCharLength = StoredText.Length;
		char[] messageCharacters = StoredText.ToCharArray();
		if ((Object)(object)storedchatboxtext != (Object)null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex] != null && storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action.Length != 0 && (Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex] != (Object)null && !storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex].RunActionOnChatEnd)
		{
			CHATBOXACTION cHATBOXACTION = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Action[CurrentTextIndex];
			if (cHATBOXACTION.PlaySound && cHATBOXACTION.PossibleSounds.Length != 0)
			{
				AudioClip val = cHATBOXACTION.PossibleSounds[Random.Range(0, cHATBOXACTION.PossibleSounds.Length)];
				TextVoiceEmitter.PlayOneShot(val);
			}
			GameObject val2 = GameObject.Find(cHATBOXACTION.TargetComponentGameObjectName);
			if (cHATBOXACTION.RunComponentFunction && (Object)(object)val2 != (Object)null && cHATBOXACTION.FunctionName != null)
			{
				string targetComponentClassname = cHATBOXACTION.TargetComponentClassname;
				Component component = val2.GetComponent(targetComponentClassname);
				if ((Object)(object)component != (Object)null)
				{
					if (((object)component).GetType().GetMethod(cHATBOXACTION.FunctionName) != null)
					{
						((object)component).GetType().GetMethod(cHATBOXACTION.FunctionName).Invoke(component, null);
					}
					else
					{
						MonoBehaviour.print((object)"did you forget to make the method public?");
					}
				}
				else
				{
					MonoBehaviour.print((object)("Couldn't find Component named: " + targetComponentClassname));
				}
			}
		}
		AttemptRunSubActions();
		while (MaxVisibleCharacters < messageCharLength)
		{
			if (messageCharacters[MaxVisibleCharacters].ToString() == " ")
			{
				MaxVisibleCharacters++;
				yield return (object)new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters] == '<')
			{
				int num = MaxVisibleCharacters;
				int num2 = StoredText.IndexOf('>', num);
				if (num2 != -1)
				{
					int num3 = num2 - num + 1;
					MaxVisibleCharacters += num3;
					Text_SubtractedRichText += num3;
				}
			}
			else if (messageCharacters[MaxVisibleCharacters].ToString() == "(")
			{
				MaxVisibleCharacters++;
				yield return (object)new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else if (messageCharacters[MaxVisibleCharacters].ToString() == ")")
			{
				MaxVisibleCharacters++;
				yield return (object)new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == "@")
			{
				StoredText = RemoveSelectedCharacter(StoredText, MaxVisibleCharacters);
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				yield return (object)new WaitForSeconds(0.125f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == "#")
			{
				StoredText = RemoveSelectedCharacter(StoredText, MaxVisibleCharacters);
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				yield return (object)new WaitForSeconds(0.5f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == ";")
			{
				StoredText = RemoveSelectedCharacter(StoredText, MaxVisibleCharacters);
				StoredText = AddSelectedCharacter(StoredText, MaxVisibleCharacters, "\n");
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				if ((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex] != (Object)null)
				{
					if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].GiveCharacterBulletpoint)
					{
						TextMeshProUGUI textBulletpointUI = TextBulletpointUI;
						((TMP_Text)textBulletpointUI).text = ((TMP_Text)textBulletpointUI).text + "\n*";
					}
					else
					{
						TextMeshProUGUI textBulletpointUI2 = TextBulletpointUI;
						((TMP_Text)textBulletpointUI2).text = ((TMP_Text)textBulletpointUI2).text + "\n";
					}
				}
				else
				{
					TextMeshProUGUI textBulletpointUI3 = TextBulletpointUI;
					((TMP_Text)textBulletpointUI3).text = ((TMP_Text)textBulletpointUI3).text + "\n*";
				}
				MaxVisibleCharacters++;
				yield return (object)new WaitForSeconds(0.2f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters] == '|')
			{
				int startIndex = MaxVisibleCharacters + 1;
				int endIndex;
				for (endIndex = startIndex; endIndex < StoredText.Length && char.IsDigit(StoredText[endIndex]); endIndex++)
				{
					yield return null;
				}
				if (int.TryParse(StoredText.Substring(startIndex, endIndex - startIndex), out var result) && result >= 0 && result < StoredAdditiveValues.Count)
				{
					string value = StoredAdditiveValues[result];
					StoredText = StoredText.Remove(MaxVisibleCharacters, endIndex - MaxVisibleCharacters);
					StoredText = StoredText.Insert(MaxVisibleCharacters, value);
					((TMP_Text)TextUI).text = StoredText;
					CurrentText = StoredText;
					messageCharLength = StoredText.Length;
					messageCharacters = StoredText.ToCharArray();
					MaxVisibleCharacters++;
					yield return (object)new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
				}
				else
				{
					Debug.LogError((object)"Invalid index after '|' or index out of range.");
					MaxVisibleCharacters++;
					yield return (object)new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
				}
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == "~")
			{
				StoredText = RemoveSelectedCharacter(StoredText, MaxVisibleCharacters);
				StoredText = AddSelectedCharacter(StoredText, MaxVisibleCharacters, "\n");
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				if ((Object)(object)storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex] != (Object)null)
				{
					if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Character[CurrentTextIndex].GiveCharacterBulletpoint)
					{
						TextMeshProUGUI textBulletpointUI4 = TextBulletpointUI;
						((TMP_Text)textBulletpointUI4).text = ((TMP_Text)textBulletpointUI4).text + "\n\n*";
					}
					else
					{
						TextMeshProUGUI textBulletpointUI5 = TextBulletpointUI;
						((TMP_Text)textBulletpointUI5).text = ((TMP_Text)textBulletpointUI5).text + "\n";
					}
				}
				else
				{
					TextMeshProUGUI textBulletpointUI6 = TextBulletpointUI;
					((TMP_Text)textBulletpointUI6).text = ((TMP_Text)textBulletpointUI6).text + "\n\n*";
				}
				MaxVisibleCharacters++;
				yield return (object)new WaitForSeconds(0.2f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == ",")
			{
				MaxVisibleCharacters++;
				yield return (object)new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == ".")
			{
				MaxVisibleCharacters++;
				yield return (object)new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == "&")
			{
				StoredText = RemoveSelectedCharacter(StoredText, MaxVisibleCharacters);
				StoredText = AddSelectedCharacter(StoredText, MaxVisibleCharacters, Environment.UserName.ToUpper());
				((TMP_Text)TextUI).text = StoredText;
				CurrentText = StoredText;
				messageCharLength = StoredText.Length;
				messageCharacters = StoredText.ToCharArray();
				yield return (object)new WaitForSeconds(0f);
			}
			else if (StoredText[MaxVisibleCharacters].ToString() == "£")
			{
				CurrentText = "";
				((TMP_Text)TextUI).text = "";
				MaxVisibleCharacters = 0;
				((TMP_Text)TextUI).maxVisibleCharacters = 0;
				CurrentTextIndex++;
				((MonoBehaviour)this).StopCoroutine("PlayText");
				if (CurrentTextIndex + 1 < storedchatboxtext.Textboxes[StoredAfterIndex].Text.Length)
				{
					if ((Object)(object)storedreciever != (Object)null)
					{
						RunText(storedchatboxtext, StoredAfterIndex, storedreciever, ResetCurrentTextIndex: false);
					}
					else
					{
						RunText(storedchatboxtext, StoredAfterIndex, null, ResetCurrentTextIndex: false);
					}
				}
				else
				{
					EndText();
				}
			}
			else if (!ForcedFinishText)
			{
				MaxVisibleCharacters++;
				TextVoiceEmitter.PlayOneShot(TextVoiceEmitter.clip);
				yield return (object)new WaitForSeconds(0.0265f * CurrentTextSpeedMultiplier);
			}
			else
			{
				MaxVisibleCharacters++;
			}
			((TMP_Text)TextUI).maxVisibleCharacters = MaxVisibleCharacters - Text_SubtractedRichText;
		}
		if ((Object)(object)storedreciever != (Object)null)
		{
			storedreciever.CurrentlyBeingUsed = false;
		}
		StartShowingReactions();
	}

	private string RemoveSelectedCharacter(string text, int Index)
	{
		return text.Remove(Index, 1);
	}

	private string AddSelectedCharacter(string text, int Index, string Insert)
	{
		return text.Insert(Index, Insert);
	}

	public int CountOfStringInText(string TargetText, char Counter)
	{
		int num = 0;
		bool flag = false;
		char[] array = TargetText.ToCharArray();
		for (int i = 0; i < array.Length; i++)
		{
			if (array[i] == Counter)
			{
				flag = true;
			}
			else if (false)
			{
				num++;
			}
		}
		return num;
	}

	public void EnableTextInput()
	{
		AllowInput = true;
	}

	public void DisableTextInput()
	{
		AllowInput = false;
	}

	public static string RemoveRichTextTags(string input)
	{
		string pattern = "<.*?>";
		return Regex.Replace(input, pattern, "");
	}

	private IEnumerator PlayTextBackup()
	{
		string StoredText = storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text[CurrentTextIndex];
		((TMP_Text)TextUI).text = "* ";
		string currentText = CurrentText;
		for (int i = 0; i < currentText.Length; i++)
		{
			char c = currentText[i];
			if (storedchatboxtext.Textboxes[CurrentAdditionalTextIndex].Text[CurrentTextIndex] == StoredText)
			{
				if (c.ToString() == "#")
				{
					yield return (object)new WaitForSeconds(0.5f);
				}
				else if (c.ToString() == "@")
				{
					yield return (object)new WaitForSeconds(0.125f);
				}
				else if (c.ToString() == ",")
				{
					TextMeshProUGUI textUI = TextUI;
					((TMP_Text)textUI).text = ((TMP_Text)textUI).text + c;
					yield return (object)new WaitForSeconds(0.2f);
				}
				else if (c.ToString() == ".")
				{
					TextMeshProUGUI textUI2 = TextUI;
					((TMP_Text)textUI2).text = ((TMP_Text)textUI2).text + c;
					yield return (object)new WaitForSeconds(0.02f);
				}
				else if (c.ToString() == "?")
				{
					TextMeshProUGUI textUI3 = TextUI;
					((TMP_Text)textUI3).text = ((TMP_Text)textUI3).text + c;
					yield return (object)new WaitForSeconds(0.02f);
				}
				else if (c.ToString() == "!")
				{
					TextMeshProUGUI textUI4 = TextUI;
					((TMP_Text)textUI4).text = ((TMP_Text)textUI4).text + c;
					yield return (object)new WaitForSeconds(0.02f);
				}
				else if (c.ToString() == " ")
				{
					TextMeshProUGUI textUI5 = TextUI;
					((TMP_Text)textUI5).text = ((TMP_Text)textUI5).text + c;
					yield return (object)new WaitForSeconds(0.02f);
				}
				else
				{
					TextMeshProUGUI textUI6 = TextUI;
					((TMP_Text)textUI6).text = ((TMP_Text)textUI6).text + c;
					TextVoiceEmitter.Stop();
					TextVoiceEmitter.PlayOneShot(TextVoiceEmitter.clip);
					yield return (object)new WaitForSeconds(0.02f);
				}
			}
		}
	}

	public int CountRichTextTagCharacters(string input)
	{
		string pattern = "<.*?>";
		MatchCollection matchCollection = Regex.Matches(input, pattern);
		int num = 0;
		foreach (Match item in matchCollection)
		{
			num += item.Length;
		}
		return num;
	}
}
